package string ;
import ColorConverter.Color;

class Css {
	public var text_align:String;
	public var font_size: Float;
	var text_decoration:String;
	var margin:String;
	public var margin_top: Float;
	public var margin_bottom: Float;
	public var margionclickeft: Float;
	public var margin_right: Float;
	
	var padding:String;
/*
	public var padding_top: Float=0;
	public var padding_bottom: Float=0;
	public var padding_left: Float=0;
	public var padding_right: Float=0;
*/
	public var padding_top: Float;
	public var padding_bottom: Float;
	public var padding_left: Float;
	public var padding_right: Float;
	
	public var font_weight:String;
	public var font_style:String;
	public var font_family:String;
	public var color: Float;
	var stop_process: Float;  // Flag for disable checking
	public var background_color: Float;
	public var background_color_set:Bool;
	
	var display:String;
	
	public function new( txt:String ) {
		
		padding_top = 0;
		padding_bottom = 0;
		padding_left = 0;
		padding_right = 0;


		// To lower case
		txt.toLowerCase();
		
		// monk.e.boy: remove the { and }
		txt = StringTools.replace(txt, '{', '' );
		txt = StringTools.replace(txt, '}', '' );
		
		// monk.e.boy: setup some default values.
		// does this confilct with 'clear()'?
		this.margin_top		= 0;
		this.margin_bottom	= 0;
		this.margionclickeft	= 0;
		this.margin_right	= 0;
		
		this.padding_top	= 0;
		this.padding_bottom	= 0;
		this.padding_left	= 0;
		this.padding_right	= 0;
	
		this.color = 0;
		this.background_color_set = false;
		
		// Splitting by the ;
		var arr:Array<String> = txt.split(";");
		
		// Checking all the types of css params we accept and writing to Internal variables of the object class
		for ( i in 0...arr.length )
		{
			getAttribute(arr[i]);
		}
	}
/*		
	function trim( txt:String ):String {
		var l: Float = 0;
		var r: Float = txt.length - 1;
		while(txt.charAt(l) == ' ' || txt.charAt(l) == "\t" ) l++;
		while(txt.charAt(r) == ' ' || txt.charAt(r) == "\t" ) r--;
		return txt.substr( l, r+1 );
	}
*/
	
	function removeDoubleSpaces( txt:String ):String {
		var aux:String;
		var auxPrev:String;
		aux = txt;
		do {
			auxPrev = aux;
			StringTools.replace(aux,'  ',' '); 
		} while (  auxPrev.length != aux.length  );
		return aux;
	}
	
	function ToFloat(cad:String): Float {
		
		cad = StringTools.replace(cad, 'px', '' );
		if ( Math.isNaN( Std.parseFloat(cad) )  )
			return 0;
		return Std.parseFloat(cad);
	}
	
	function getAttribute( txt:String ):Void {
		var arr:Array<String> = txt.split(":");
		if( arr.length==2 )
		{
			this.stop_process = 1;
			this.set( arr[0], StringTools.trim(arr[1]) );
		}
	}
	/*
	public function get( cad:String ): Float {
		return switch (cad) {
			case "text-align"			: this.text_align;
			case "font-size"			: ToFloat(this.font_size);
			case "text-decoration"		: this.text_decoration;
			case "margin-top"			: this.margin_top;
			case "margin-bottom"		:  this.margin_bottom;
			case "margionclickeft"			: this.margionclickeft;
			case "margin-right"			: this.margin_right;
			case "padding-top"			: this.padding_top;
			case "padding-bottom"		: this.padding_bottom;
			case "padding-left"			: this.padding_left;
			case "padding-right"		: this.padding_right;
			case "font-weight"			: ToFloat(this.font_weight);
			case "font-style"			: this.font_style;
			case "font-family"			: this.font_family;
			case "color"				: this.color;
			case "background-color"		: this.bg_color;
			case "display"				: this.display;
			default						: 0;
		}
	}
	*/
	// FUCKING!! Flash without By reference String parameters on functions
	public function set( cad:String, val:String ):Void {
		cad = StringTools.trim( cad );
	
		switch( cad )
		{
			case "text-align"			: this.text_align = val;			/*break*/
			case "font-size"			: this.set_font_size(val);			/*break*/
			case "text-decoration"		: this.text_decoration = val;		/*break*/
			
			case "margin"				: this.setMargin(val);			/*break*/
			case "margin-top"			: this.margin_top = ToFloat(val); /*break*/
			case "margin-bottom"		: this.margin_bottom = ToFloat(val); /*break*/
			case "margionclickeft"			: this.margionclickeft = ToFloat(val); /*break*/
			case "margin-right"			: this.margin_right = ToFloat(val); /*break*/
			
			case 'padding'				: this.setPadding(val);				/*break*/
			case "padding-top"			: this.padding_top = ToFloat(val); /*break*/
			case "padding-bottom"		: this.padding_bottom = ToFloat(val); /*break*/
			case "padding-left"			: this.padding_left = ToFloat(val); /*break*/
			case "padding-right"		: this.padding_right = ToFloat(val); /*break*/
			
			case "font-weight"			: this.font_weight = val; /*break*/
			case "font-style"			: this.font_style = val; /*break*/
			case "font-family"			: this.font_family = val; /*break*/
			case "color"				: this.color = convertColor(Hex(val)); //this.set_color(val);				/*break*/
			case "background-color":
				this.background_color = convertColor(Hex(val));
				this.background_color_set = true;
				/*break*/
			case "display"				: this.display = val; /*break*/
		}
	}
	
// 	public function set_color( val:String ):Void {
// 		this.color = Utils.get_color( val );
// 	}

	function convertColor (c : Color) : Float {
		return ColorConverter.toFloat(c);
	}
	
	public function set_font_size( val:String ):Void {
		this.font_size = ToFloat(val);
	}
	
	
	function setPadding( val:String ):Void {

		val = StringTools.trim( val );
		var arr:Array<String> = val.split(' ');
		
		switch( arr.length )
		{
			
			// margin: 30px;
			case 1:
				this.padding_top	= ToFloat(arr[0]);
				this.padding_right	= ToFloat(arr[0]);
				this.padding_bottom	= ToFloat(arr[0]);
				this.padding_left	= ToFloat(arr[0]);
				/*break*/
				
			// margin: 15px 5px;
			case 2:
				this.padding_top	= ToFloat(arr[0]);
				this.padding_right	= ToFloat(arr[1]);
				this.padding_bottom	= ToFloat(arr[0]);
				this.padding_left	= ToFloat(arr[1]);
				/*break*/
				
			// margin: 15px 5px 10px;
			case 3:
				this.padding_top	= ToFloat(arr[0]);
				this.padding_right	= ToFloat(arr[1]);
				this.padding_bottom	= ToFloat(arr[2]);
				this.padding_left	= ToFloat(arr[1]);
				/*break*/
				
			// margin: 1px 2px 3px 4px;
			default:
				this.padding_top	= ToFloat(arr[0]);
				this.padding_right	= ToFloat(arr[1]);
				this.padding_bottom	= ToFloat(arr[2]);
				this.padding_left	= ToFloat(arr[3]);
		}
	}
	
	function setMargin( val:String ):Void {

		val = StringTools.trim( val );
		var arr:Array<String> = val.split(' ');
		
		switch( arr.length )
		{
			// margin: 30px;
			case 1:
				this.margin_top		= ToFloat(arr[0]);
				this.margin_right	= ToFloat(arr[0]);
				this.margin_bottom	= ToFloat(arr[0]);
				this.margionclickeft	= ToFloat(arr[0]);
				/*break*/
			
			// margin: 15px 5px;
			case 2:
				this.margin_top		= ToFloat(arr[0]);
				this.margin_right	= ToFloat(arr[1]);
				this.margin_bottom	= ToFloat(arr[0]);
				this.margionclickeft	= ToFloat(arr[1]);
				/*break*/
				
			// margin: 15px 5px 10px;
			case 3:
				this.margin_top		= ToFloat(arr[0]);
				this.margin_right	= ToFloat(arr[1]);
				this.margin_bottom	= ToFloat(arr[2]);
				this.margionclickeft	= ToFloat(arr[1]);
				/*break*/
				
			// margin: 1px 2px 3px 4px;
			default:
				this.margin_top		= ToFloat(arr[0]);
				this.margin_right	= ToFloat(arr[1]);
				this.margin_bottom	= ToFloat(arr[2]);
				this.margionclickeft	= ToFloat(arr[3]);
		}
	}
	
	public function clear():Void {
		this.text_align			= null;
		this.font_size			= null;
		this.text_decoration		= null;
		this.margin_top			= null;
		this.margin_bottom		= null;
		this.margionclickeft		= null;
		this.margin_right		= null;
		this.font_weight		= null;
		this.font_style			= null;
		this.font_family		= null;
		this.color			= null;
		this.display			= null;
	}
}
